/* 
 * 	This file is part of VPN Monitor Project 7..

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package monitor.utility;

import java.util.Calendar;
import java.util.Properties;

/**
 * Singleton class for getting the time.
 */
public class Time{
	private static Time singleton = null;
	private static final String[] NAMES = {"today","week","month","year"};
	
	private Time(){}
	
	/**
	 * An instance of the class.
	 */
	public synchronized static Time getInstance(){
		if(Time.singleton==null)
			Time.singleton = new Time();
		return Time.singleton;
	}
	
	/**
	 * For unpacking time stored in a Properties file.
	 * @param time The file from which the date values should be unpacked from.
	 * @return
	 */
	public int[] getStoredDates(Properties time){
		int[] dates = new int[Time.NAMES.length];
		for(int i=0;i<Time.NAMES.length;i++)
			dates[i] = ((Integer) time.get(Time.NAMES[i])).intValue();
		return dates;
	}
	
	/**
	 * Packing the current dates into a Properties file.
	 * @return Date values as Properties file.
	 */
	public Properties storeDates(){
		Properties properties = new Properties();
		int[] dates = getDates();
		for(int i=0;i<Time.NAMES.length;i++){
			properties.setProperty(Time.NAMES[i], Integer.toString(dates[i]));
		}
		return properties;
	}
		
	/**
	 * For getting the current dates value in a
	 * size four array, with the date values for <br>
	 * this day of year,<br> week of year,<br> month,<br> year <br>
	 * in that order.
	 * @return An array containing the four different date values.
	 */
	public int[] getDates(){
		int[] dateKeys = {
				Calendar.DAY_OF_YEAR,
				Calendar.WEEK_OF_YEAR,
				Calendar.MONTH,
				Calendar.YEAR
		};
		Calendar cal = Calendar.getInstance();
		int[] dates = new int[dateKeys.length];
		for(int i=0;i<dateKeys.length;i++)
			dates[i] = cal.get(dateKeys[i]);
		return dates;
	}
}
